#include "Colour.h"

Colour::Colour()
{
	r = 0.5f;
	g = 0.5f;
	b = 0.5f;
	a = 1.0f;
	gloss = 0.0f;
}

Colour::Colour(float red, float green, float blue, float alpha)
{
	r = red;
	g = green;
	b = blue;
	a = alpha;
	gloss = 0.0f;
}

void Colour::SetColour(float red, float green, float blue, float alpha)
{
	r = red;
	g = green;
	b = blue;
	a = alpha;
	gloss = 0.0f;
}

float Colour::Brightness()
{
	return (r + g + b) / 3.0f;
}

Colour Colour::Scalar(float scalar)
{
	return Colour(r * scalar, g * scalar, b * scalar, a);
}

Colour Colour::AverageColours(const Colour& col)
{
	return Colour((r + col.r) / 2.0f, (g + col.g) / 2.0f, (b + col.b) / 2.0f, a);
}

Colour Colour::Clip()
{
	float total = r + g + b;
	float excess = total - 3.0f;
	if (excess > 0.0f)
	{
		r = r + excess * (r / total);
		g = g + excess * (g / total);
		b = b + excess * (b / total);
	}

	if (r > 1.0f)
	{
		r = 1.0f;
	}
	if (g > 1.0f)
	{
		g = 1.0f;
	}
	if (b > 1.0f)
	{
		b = 1.0f;
	}
	
	if (r < 0.0f)
	{
		r = 0.0f;
	}
	if (g < 0.0f)
	{
		g = 0.0f;
	}
	if (b < 0.0f)
	{
		b = 0.0f;
	}

	return *this;
}